package vn.edu.uit.service.fetchers;

import java.util.ArrayList;

import org.json.JSONObject;

import vn.edu.uit.hotelbooking.JsonParse.RoomListParser;
import vn.edu.uit.hotelbooking.model.Room;
import vn.edu.uit.service.UITAPIContant;
import vn.edu.uit.service.UITHttpRequest;
import vn.edu.uit.service.UITHttpRequest.RequestMethod;
import vn.edu.uit.service.UITService;
import android.util.Log;

public class RoomListFetcher {
    private static UITHttpRequest httpRequest;
    private static final String LOG_TAG = "RoomListFetcher";
    private static final String TAG_HOTEL_ID = "id";
    private static final String TAG_CHECKIN = "checkin";
    private static final String TAG_CHECKOUT = "checkout";
    
    /**
     * The method to get the list of room of a hotel
     * @param hotelId the id of room's hotel
     * @param checkIn the checkin date
     * @param checkOut the checkout date
     * @return If success, return an Arraylist<Room> contains the rooms. If failed, return null. 
     */
    public static ArrayList<Room> getRoomList(String hotelId, String checkIn, String checkOut) {
        UITService.REQUEST_SUCCESS = false;
        httpRequest=new UITHttpRequest(UITAPIContant.API_REST_DOMAIN_URL+UITAPIContant.Api_Functions.GET_ROOM_LIST);
        httpRequest.addParam(UITService.TAG_AUTHENTICATE, UITService.TOKEN);
        httpRequest.addParam(TAG_HOTEL_ID, hotelId);
        httpRequest.addParam(TAG_CHECKIN, checkIn);
        httpRequest.addParam(TAG_CHECKOUT, checkOut);
        try {
            JSONObject object = httpRequest.doExecute(RequestMethod.POST);
            Log.d("JSON", object.toString());
            if(object.getInt(UITService.TAG_ERROR)==0) {       //request was successful, return a room arraylist
                UITService.REQUEST_SUCCESS = true;
                return RoomListParser.getRoomListFromJson(object.getJSONArray(UITService.TAG_DATA));
            } else {                                //request was failed, return null
                UITService.REQUEST_SUCCESS = false;
                UITService.MSG = object.getString(UITService.TAG_MSG);
                return null;
            }   
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(LOG_TAG, e.getMessage());
        }
        return null;
    }   
}
